
package ui.adapter;

import android.content.Context;
import android.graphics.Color;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.text.Spanned;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.alibaba.android.vlayout.DelegateAdapter;
import com.alibaba.android.vlayout.LayoutHelper;
import com.alibaba.android.vlayout.layout.SingleLayoutHelper;

import net.ck.hzq.R;

import java.util.ArrayList;
import java.util.List;

import ui.model.ProfitpromotionBean;


/**
 * 当日收益金钱适配器
 * Created by chenjian on 19/2/27
 */
public class ProfitMoneyAdapter extends DelegateAdapter.Adapter<ProfitMoneyAdapter.ProfitMoneyVH> {

    private final Context mContext;
    private List<ProfitpromotionBean.RecordsBean> mData;

    public ProfitMoneyAdapter(Context context) {
        mContext = context;
        mData = new ArrayList<>();
    }

    public void setupData(List<ProfitpromotionBean.RecordsBean> data) {
        mData.clear();
        if (data != null) {
            mData.addAll(data);
        }
        notifyDataSetChanged();
    }

    public List<ProfitpromotionBean.RecordsBean> getData() {
        return mData;
    }

    @Override
    public LayoutHelper onCreateLayoutHelper() {
        return new SingleLayoutHelper();
    }

    @NonNull
    @Override
    public ProfitMoneyVH onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        return new ProfitMoneyVH(LayoutInflater.from(mContext).inflate(R.layout.item_profitpromotion, viewGroup, false));
    }

    @Override
    public void onBindViewHolder(@NonNull ProfitMoneyVH holder, int position) {
        holder.amount.setText("交易金额：" + mData.get(position).getAmount());
        holder.reward.setText("商户编号：" + mData.get(position).getMerchantNo());
        holder.merchant.setText(mData.get(position).getTransDate());
        if (mData.get(position).getMerchantLevel().equals("1")) {
            holder.amount.setTextColor(Color.parseColor("#17AB28"));
            holder.reward.setTextColor(Color.parseColor("#17AB28"));
            holder.merchant.setTextColor(Color.parseColor("#17AB28"));
            Spanned strB = Html.fromHtml("<font color=#17AB28>" +mData.get(position).getDivitedType() + "</font>：<font color=#FF2F2F>" + mData.get(position).getIncome() + "</font>");
            if (!mData.get(position).getIncome().contains("-")) {
                strB = Html.fromHtml("<font color=#17AB28>" +mData.get(position).getDivitedType() + "</font>：<font color=#FF2F2F>" + "+" + mData.get(position).getIncome() + "</font>");
            }
            holder.income.setText(strB);
        } else if (mData.get(position).getMerchantLevel().equals("2")) {
            holder.amount.setTextColor(Color.parseColor("#F86912"));
            holder.reward.setTextColor(Color.parseColor("#F86912"));
            holder.merchant.setTextColor(Color.parseColor("#F86912"));
            Spanned strB = Html.fromHtml("<font color=#F86912>" +mData.get(position).getDivitedType() + "</font>：<font color=#FF2F2F>" + mData.get(position).getIncome() + "</font>");
            if (!mData.get(position).getIncome().contains("-")) {
                strB = Html.fromHtml("<font color=#F86912>" +mData.get(position).getDivitedType() + "</font>：<font color=#FF2F2F>" + "+" + mData.get(position).getIncome() + "</font>");
            }
            holder.income.setText(strB);
        } else if (mData.get(position).getMerchantLevel().equals("3")) {
            holder.amount.setTextColor(Color.parseColor("#333333"));
            holder.reward.setTextColor(Color.parseColor("#333333"));
            holder.merchant.setTextColor(Color.parseColor("#333333"));
            Spanned strB = Html.fromHtml("<font color=#333333>" +mData.get(position).getDivitedType() + "</font>：<font color=#FF2F2F>" + mData.get(position).getIncome() + "</font>");
            if (!mData.get(position).getIncome().contains("-")) {
                strB = Html.fromHtml("<font color=#333333>" +mData.get(position).getDivitedType() + "</font>：<font color=#FF2F2F>" + "+" + mData.get(position).getIncome() + "</font>");
            }
            holder.income.setText(strB);
        } else if (mData.get(position).getMerchantLevel().equals("4")) {
            holder.amount.setTextColor(Color.parseColor("#1232F8"));
            holder.reward.setTextColor(Color.parseColor("#1232F8"));
            holder.merchant.setTextColor(Color.parseColor("#1232F8"));
            Spanned strB = Html.fromHtml("<font color=#1232F8>" +mData.get(position).getDivitedType() + "</font>：<font color=#FF2F2F>" + mData.get(position).getIncome() + "</font>");
            if (!mData.get(position).getIncome().contains("-")) {
                strB = Html.fromHtml("<font color=#1232F8>" +mData.get(position).getDivitedType() + "</font>：<font color=#FF2F2F>" + "+" + mData.get(position).getIncome() + "</font>");
            }
            holder.income.setText(strB);
        } else {
            holder.amount.setTextColor(Color.parseColor("#333333"));
            holder.reward.setTextColor(Color.parseColor("#333333"));
            holder.merchant.setTextColor(Color.parseColor("#333333"));
            Spanned strB = Html.fromHtml("<font color=#333333>" +mData.get(position).getDivitedType() + "</font>：<font color=#FF2F2F>" + mData.get(position).getIncome() + "</font>");
            if (!mData.get(position).getIncome().contains("-")) {
                strB = Html.fromHtml("<font color=#333333>" +mData.get(position).getDivitedType() + "</font>：<font color=#FF2F2F>" + "+" + mData.get(position).getIncome() + "</font>");
            }
            holder.income.setText(strB);
        }
        if (mData.get(position).getOperationType().equals("0")) {
            holder.paytype.setText(mData.get(position).getTradeType());
            holder.paytype.setBackground(mContext.getResources().getDrawable(R.drawable.bg_profitpromotion));
        } else if (mData.get(position).getOperationType().equals("1")) {
            holder.paytype.setBackground(mContext.getResources().getDrawable(R.drawable.bg_profitpromotion2));
            holder.paytype.setText("冲正");
        } else if (mData.get(position).getOperationType().equals("2")) {
            holder.paytype.setBackground(mContext.getResources().getDrawable(R.drawable.bg_profitpromotion2));
            holder.paytype.setText("撤销");
        } else {
            holder.paytype.setText(mData.get(position).getTradeType());
            holder.paytype.setBackground(mContext.getResources().getDrawable(R.drawable.bg_profitpromotion));
        }
        if (mData.get(position).getTradeType().equals("")) {
            holder.paytype.setVisibility(View.GONE);
        } else {
            holder.paytype.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    class ProfitMoneyVH extends RecyclerView.ViewHolder {
        private TextView amount;
        private TextView income;
        private TextView reward;
        private TextView merchant;
        private TextView paytype;

        ProfitMoneyVH(@NonNull View itemView) {
            super(itemView);
            amount = itemView.findViewById(R.id.item_profit_name);
            income = itemView.findViewById(R.id.item_profit_money);
            reward = itemView.findViewById(R.id.item_profit_type);
            merchant = itemView.findViewById(R.id.item_profit_time);
            paytype = itemView.findViewById(R.id.item_profit_trade);
        }
    }
}
